﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WPFEdit.ViewModels;

namespace WPFEdit.Document.ViewModels
{
	/// <summary>
	/// An obejct to act as the data context for the document tab view, as well as the
	/// record of a managed document.
	/// </summary>
    public class OpenDocumentInfo : TabItem
    {
        private IDocumentEditor m_editor;
        private Document m_document;
        private IDocumentWriter m_writer;

        private bool m_hasUnsavedChanges;

		/// <summary>
		/// Gets or sets a value indicating if the document has unsaved changes. If
		/// this value is changes, it will change the tab name to have a * and raise 
		/// the property changed event.
		/// </summary>
        public bool HasUnsavedChanges
        {
            get { return m_hasUnsavedChanges; }
            set
            {
                if (m_hasUnsavedChanges != value)
                {
                    if (m_document.DocumentData is IEditable)
                    {
                        ((IEditable)m_document.DocumentData).Dirty = false;
                    }
                    m_hasUnsavedChanges = value;
                    OnPropertyChanged("TabName");
                    OnPropertyChanged("HasUnsavedChanges");
                }
            }
        }

		/// <summary>
		/// The writer for this document, can be null.
		/// </summary>
        public IDocumentWriter Writer
        {
            get { return m_writer; }
            set { m_writer = value; }
        }

		/// <summary>
		/// Gets or sets the document. This will monitor the set document for changes.
		/// </summary>
        public Document Document
        {
            get { return m_document; }
            set
            {
                if (m_document != null) m_document.DataChanged -= DocumentDataChanged;
                m_document = value;
                m_document.DataChanged += DocumentDataChanged;
            }
        }

        void DocumentDataChanged(object sender, EventArgs e)
        {
            HasUnsavedChanges = true;
        }

		/// <summary>
		/// The editor for the document.
		/// </summary>
        public IDocumentEditor Editor
        {
            get { return m_editor; }
            set { m_editor = value; }
        }

		/// <summary>
		/// Returns a string for the tab name. Setting will cause an exception.
		/// </summary>
        public override string TabName
        {
            get
            {
                return m_document.FileName+(HasUnsavedChanges ? "*" : String.Empty);
            }
            set
            {
                throw new InvalidOperationException("Tab Name for a document tab is defined by the Tab's Document. It cannot be set directly.");
            }
        }

		/// <summary>
		/// Returns the control for the tab.
		/// </summary>
        public override object TabContent
        {
            get
            {
                return m_editor.ContentControl;
            }
            set
            {
                throw new InvalidOperationException("Tab Content for a document tab is defined by the Tab's Editor. It cannot be set directly.");
            }
        }
    }

}
